package cn.wolfcode.p2p.bussness.service.impl;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.bussness.domain.ExpAccount;
import cn.wolfcode.p2p.bussness.domain.ExpAccountFlow;
import cn.wolfcode.p2p.bussness.domain.ExpAccountGrantRecord;
import cn.wolfcode.p2p.bussness.mapper.ExpAccountFlowMapper;
import cn.wolfcode.p2p.bussness.mapper.ExpAccountGrantRecordMapper;
import cn.wolfcode.p2p.bussness.mapper.ExpAccountMapper;
import cn.wolfcode.p2p.bussness.service.IExpAccountService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ExpAccountServiceImpl implements IExpAccountService {

    @Autowired
    private ExpAccountMapper expAccountMapper;

    @Autowired
    private ExpAccountGrantRecordMapper recordMapper;

    @Autowired
    private ExpAccountFlowMapper expAccountFlowMapper;

    @Override
    public int save(ExpAccount expAccount) {
        return expAccountMapper.insert(expAccount);
    }

    @Override
    public int update(ExpAccount expAccount) {
        int count = expAccountMapper.updateByPrimaryKey(expAccount);
        if (count == 0) {
            throw new DisplayableException("乐观锁异常,乐观锁Id为: " + expAccount.getId());
        }
        return count;
    }

    @Override
    public ExpAccount get(Long id) {
        return expAccountMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryPage(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<ExpAccount> list = expAccountMapper.selectList(qo);
        return new PageInfo(list);
    }

    @Override
    public void grantExpMoney(Long id, LastTime lastTime, BigDecimal expMoney, int expMoneyType) {
        //创建一个发放回收记录对象
        ExpAccountGrantRecord record = new ExpAccountGrantRecord();
        //发放金额
        record.setAmount(expMoney);
        //发放时间
        record.setGrantDate(new Date());
        //发放类型
        record.setGrantType(expMoneyType);
        //对应哪一个用户
        record.setGrantUserId(id);
        //说明
        record.setNote("注册发放体验金");
        //到期时间
        record.setReturnDate(lastTime.getReturnDate(new Date()));
        //当前状态
        record.setState(ExpAccountGrantRecord.STATE_NORMAL);
        //保存到数据库
        this.recordMapper.insert(record);

        //更新体验金账户
        ExpAccount expAccount = this.expAccountMapper.selectByPrimaryKey(id);
        //可用体验金 = 之前的体验金 + 后面加上的体验金
        expAccount.setUsableAmount(expAccount.getUsableAmount().add(expMoney));
        //更新到数据库
        this.expAccountMapper.updateByPrimaryKey(expAccount);

        //增加一条发放体验金流水
        ExpAccountFlow flow = new ExpAccountFlow();
        //交易时间
        flow.setActionTime(new Date());
        //交易金额
        flow.setAmount(expMoney);
        //体验金用户
        flow.setExpAccountId(id);
        //交易类型
        flow.setActionType(expMoneyType);
        //说明
        flow.setNote("注册发放体验金");
        //交易之后的体验金账户余额
        flow.setUsableAmount(expAccount.getUsableAmount());
        //交易之后的体验金账户冻结金额
        flow.setFreezedAmount(expAccount.getFreezedAmount());
        //保存到数据库
        this.expAccountFlowMapper.insert(flow);
    }
}
